/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Simulation.Messages;

import java.util.Arrays;
import Simulation.Config.Service.ServiceConfigReader;

    /*
     * Message  Layout
     *  0..10   String  Marking Text DIS String
     * 11..11           Reserved 
     * 12..12   int8    Forward Gear count 
     * 13..13   int8    Reverse Gear count
     * 14..15           Reserved
     * 16..17   int16   Max forward speed Gear  1
     * 18..19   int16   Max forward speed Gear  2
     * 20..21   int16   Max forward speed Gear  3
     * 22..23   int16   Max forward speed Gear  4
     * 24..25   int16   Max forward speed Gear  5
     * 26..27   int16   Max forward speed Gear  6
     * 28..29   int16   Max forward speed Gear  7
     * 30..31   int16   Max forward speed Gear  8
     * 32..33   int16   Max forward speed Gear  9
     * 34..35   int16   Max forward speed Gear 10 
     * 36..37   int16   Max forward speed Gear  1
     * 38..39   int16   Max forward speed Gear  2
     * 40..41   int16   Max reverse speed Gear  3
     * 42..43   int16   Max reverse speed Gear  4
     * 44..47   float32 Idle Fuel Consumption litres per KM
     * 48..51   float32 Half Speed Fuel Consumption litres per KM
     * 52..55   float32 Full Speed Fuel Consumption litres per KM
     * 56..56   int8    Allow Neutral turn
     * 57..57           Reserved
     * 58..59   int16   max turret rate degrees per second
     * 60..61   int16   max gun rate degrees per second
     * 62..63   int16   max gun elevation in degrees
     * 64..65   int16   min gun elevation in degrees
     * 66..67           Reserved
     */

public class HostConfigMessage extends Message {
    
    public HostConfigMessage(
      String  nameIn) {
        messageID   = Simulation.Messages.Message.MessageIdEnum.BEHV_HOST_CONFIG;
        messageData = new byte[MessageSize(messageID)];
        
        ServiceConfigReader cr = new ServiceConfigReader(nameIn);
        SetMarkingText(                                         cr.GetData("MARKING_TEXT"));
        SetForwardGearCount(            Byte.parseByte(         cr.GetData("FORWARD_GEAR_COUNT")));
        SetReverseGearCount(            Byte.parseByte(         cr.GetData("REVERSE_GEAR_COUNT")));
        SetForwardGear1Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_1_SPEED")));
        SetForwardGear2Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_2_SPEED")));
        SetForwardGear3Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_3_SPEED")));
        SetForwardGear4Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_4_SPEED")));
        SetForwardGear5Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_5_SPEED")));
        SetForwardGear6Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_6_SPEED")));
        SetForwardGear7Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_7_SPEED")));
        SetForwardGear8Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_8_SPEED")));
        SetForwardGear9Speed(           Short.parseShort(       cr.GetData("FORWARD_GEAR_9_SPEED")));
        SetForwardGear10Speed(          Short.parseShort(       cr.GetData("FORWARD_GEAR_10_SPEED")));
        SetReverseGear1Speed(           Short.parseShort(       cr.GetData("FORWARD_REVERSE_1_SPEED")));
        SetReverseGear2Speed(           Short.parseShort(       cr.GetData("FORWARD_REVERSE_2_SPEED")));
        SetReverseGear3Speed(           Short.parseShort(       cr.GetData("FORWARD_REVERSE_3_SPEED")));
        SetReverseGear4Speed(           Short.parseShort(       cr.GetData("FORWARD_REVERSE_4_SPEED")));
        SetIdleFuelConsumption(         Float.parseFloat(       cr.GetData("IDLE_FUEL_CONSUMPTION")));
        SetHalfSpeedFuelConsumption(    Float.parseFloat(       cr.GetData("HALF_SPEED_FUEL_CONSUMPTION")));
        SetFullSpeedFuelConsumption(    Float.parseFloat(       cr.GetData("FULL_SPEED_FUEL_CONSUMPTION")));
        SetAllowNeutralTurn(            Boolean.parseBoolean(   cr.GetData("ALLOW_NEUTRAL_TURN")));
        SetMaxTurretRate(               Short.parseShort(       cr.GetData("MAX_TURRET_RATE")));
        SetMaxGunRate(                  Short.parseShort(       cr.GetData("MAX_GUN_RATE")));
        SetMaxGunelevation(             Short.parseShort(       cr.GetData("MAX_GUN_ELE")));
        SetMinGunelevation(             Short.parseShort(       cr.GetData("MIN_GUN_ELE")));        
    }  
     
    
    public HostConfigMessage(Message msg) {
        this.messageID = msg.messageID;
        this.messageData = msg.messageData;
    }    
    

    
    public final void SetMarkingText(String name) {
        
        // Make sure name is not greater than 11 chars
        if(name.length() > 11)
            name = name.substring(0, 11);

        // Make sure name has at least 11 chars.
        while(name.length() < 11)
            name = name + " ";
        
        for(int i=0; i<11; i++)
            super.messageData[i] = name.getBytes()[i];
    }                  
    
    public final byte[] GetMarkingText() {
        return Arrays.copyOfRange(super.messageData, 0, 11);
    }      

    public final void SetForwardGearCount(byte value) {
        messageData[12] = value;
    }                  
    
    public final byte GetForwardGearCount() {
        return messageData[12];
    }      
    
    public final void SetReverseGearCount(byte value) {
        messageData[13] = value;
    }                  
    
    public final byte GetReverseGearCount() {
        return messageData[13];
    }       

    public final void SetForwardGear1Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[16] = bValue[0];
        messageData[17] = bValue[1];        
    }                  
    
    public final short GetForwardGear1Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[16];
        bValue[1] = messageData[17];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear2Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[18] = bValue[0];
        messageData[19] = bValue[1];        
    }                  
    
    public final short GetForwardGear2Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[18];
        bValue[1] = messageData[19];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear3Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[20] = bValue[0];
        messageData[21] = bValue[1];        
    }                  
    
    public final short GetForwardGear3Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[20];
        bValue[1] = messageData[21];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear4Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[22] = bValue[0];
        messageData[23] = bValue[1];        
    }                  
    
    public final short GetForwardGear4Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[22];
        bValue[1] = messageData[23];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear5Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[24] = bValue[0];
        messageData[25] = bValue[1];        
    }                  
    
    public final short GetForwardGear5Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[24];
        bValue[1] = messageData[25];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear6Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[26] = bValue[0];
        messageData[27] = bValue[1];        
    }                  
    
    public final short GetForwardGear6Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[26];
        bValue[1] = messageData[27];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear7Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[28] = bValue[0];
        messageData[29] = bValue[1];        
    }                  
    
    public final short GetForwardGear7Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[28];
        bValue[1] = messageData[29];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear8Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[30] = bValue[0];
        messageData[31] = bValue[1];        
    }                  
    
    public final short GetForwardGear8Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[30];
        bValue[1] = messageData[31];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear9Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[32] = bValue[0];
        messageData[33] = bValue[1];        
    }                  
    
    public final short GetForwardGear9Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[32];
        bValue[1] = messageData[33];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetForwardGear10Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[34] = bValue[0];
        messageData[35] = bValue[1];        
    }                  
    
    public final short GetForwardGear10Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[34];
        bValue[1] = messageData[35];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }       
   
    public final void SetReverseGear1Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[36] = bValue[0];
        messageData[37] = bValue[1];        
    }                  
    
    public final short GetReverseGear1Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[36];
        bValue[1] = messageData[37];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetReverseGear2Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[38] = bValue[0];
        messageData[39] = bValue[1];        
    }                  
    
    public final short GetReverseGear2Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[38];
        bValue[1] = messageData[39];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetReverseGear3Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[40] = bValue[0];
        messageData[41] = bValue[1];        
    }                  
    
    public final short GetReverseGear3Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[40];
        bValue[1] = messageData[41];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetReverseGear4Speed(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[42] = bValue[0];
        messageData[43] = bValue[1];        
    }                  
    
    public final short GetReverseGear4Speed() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[42];
        bValue[1] = messageData[43];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }   
    
    public final void SetIdleFuelConsumption(float value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[44] = bValue[0];
        messageData[45] = bValue[1];     
        messageData[46] = bValue[2];
        messageData[47] = bValue[3];         
    }                  
    
    public final float GetIdleFuelConsumption() {
        byte[] bValue = new byte[4];
        bValue[0] = messageData[44];
        bValue[1] = messageData[45];  
        bValue[2] = messageData[46];
        bValue[3] = messageData[47];           
        return Simulation.Utils.ByteArrayUtils.BytesToFloat(bValue);
    }       
    
    public final void SetHalfSpeedFuelConsumption(float value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[48] = bValue[0];
        messageData[49] = bValue[1];     
        messageData[50] = bValue[2];
        messageData[51] = bValue[3];         
    }                  
    
    public final float GetHalfSpeedFuelConsumption() {
        byte[] bValue = new byte[4];
        bValue[0] = messageData[48];
        bValue[1] = messageData[49];  
        bValue[2] = messageData[50];
        bValue[3] = messageData[51];           
        return Simulation.Utils.ByteArrayUtils.BytesToFloat(bValue);
    }      
    
    public final void SetFullSpeedFuelConsumption(float value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[52] = bValue[0];
        messageData[53] = bValue[1];     
        messageData[54] = bValue[2];
        messageData[55] = bValue[3];         
    }                  
    
    public final float GetFullSpeedFuelConsumption() {
        byte[] bValue = new byte[4];
        bValue[0] = messageData[52];
        bValue[1] = messageData[53];  
        bValue[2] = messageData[54];
        bValue[3] = messageData[54];           
        return Simulation.Utils.ByteArrayUtils.BytesToFloat(bValue);
    }       

    public final void SetAllowNeutralTurn(boolean value) {
        if(value)
            messageData[56] = 1;
        else
            messageData[56] = 0;
    }                  
    
    public final boolean GetAllowNeutralTurn() {         
        return (messageData[56] == 1);
    }          
    
    public final void SetMaxTurretRate(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[58] = bValue[0];
        messageData[59] = bValue[1];        
    }      
    
    public final short GetMaxTurretRate() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[58];
        bValue[1] = messageData[59];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }
    
    public final void SetMaxGunRate(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[60] = bValue[0];
        messageData[61] = bValue[1];        
    }      
    
    public final short GetMaxGunRate() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[60];
        bValue[1] = messageData[61];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }      
    
    public final void SetMaxGunelevation(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[62] = bValue[0];
        messageData[63] = bValue[1];        
    }      
    
    public final short GetMaxGunelevation() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[62];
        bValue[1] = messageData[63];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }     

    public final void SetMinGunelevation(short value) {
        byte[] bValue = Simulation.Utils.ByteArrayUtils.ToByte(value);
        messageData[64] = bValue[0];
        messageData[65] = bValue[1];        
    }      
    
    public final short GetMinGunelevation() {
        byte[] bValue = new byte[2];
        bValue[0] = messageData[64];
        bValue[1] = messageData[65];         
        return Simulation.Utils.ByteArrayUtils.BytesToShort(bValue);
    }         
    
    
    
}
